package top.fzqblog.firstapp;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import top.fzqblog.adapter.RecycleViewAdapter;

/**
 * Created by 抽离 on 2017/8/25 0025.
 */

public class RecycleViewTest extends AppCompatActivity {

    private List<String> contents = new ArrayList<>();

    private List<Integer> images = new ArrayList<Integer>(){
        {
            add(R.mipmap.bg1);
            add(R.mipmap.bg2);
            add(R.mipmap.bg3);
            add(R.mipmap.bg4);
            add(R.mipmap.bg5);
            add(R.mipmap.bg6);
            add(R.mipmap.bg7);
            add(R.mipmap.bg8);
            add(R.mipmap.bg1);
            add(R.mipmap.bg2);
            add(R.mipmap.bg3);
            add(R.mipmap.bg4);
            add(R.mipmap.bg5);
            add(R.mipmap.bg6);
            add(R.mipmap.bg7);
            add(R.mipmap.bg8);
        }
    };

    private RecycleViewAdapter recycleViewAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recycleviewtest);
        for(int i = 0; i < images.size(); i++){
            contents.add("内容" + (i + 1));
        }
        recycleViewAdapter = new RecycleViewAdapter(this, contents, images);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycleviewlayout);
        recyclerView.setAdapter(recycleViewAdapter);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //调用inflate()方法创建菜单
        getMenuInflater().inflate(R.menu.menu,menu);
        //如果返回false，创建的菜单无法显示
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item){
        //通过调用item.getItemId()来判断菜单项
        switch (item.getItemId()){
            case R.id.add_item:
                recycleViewAdapter.addItem(1);
                break;
            case R.id.remove_item:
                recycleViewAdapter.remove(1);
                break;
        }
        return true;
    }
}
